﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MVC.Models {

  public class MockStudentRepository : IStudentRepository {
    private List<Student> _studentList;
    public MockStudentRepository() {
      _studentList = new List<Student>() {
        new Student(){Id=1,Name="张三",ClassName=ClassNameEnum.FirstGrade,Email="zhang@qq.com"},
        new Student(){Id=2,Name="李四",ClassName=ClassNameEnum.SencondGrade,Email="li@qq.com"},
        new Student(){Id=3,Name="王五",ClassName=ClassNameEnum.ThirdGrade,Email="wang@qq.com"},
      };
    }

    public Student AddStudent(Student student) {
      _studentList.Add(student);
      return student;
    }

    public Student DeleteStudent(int id) {
      Student student = _studentList.Find(s => s.Id == id);
      if (student != null) {
        _studentList.Remove(student);
      }
      return student;
    }

    public Student GetStudent(int id) {
      return _studentList.FirstOrDefault(s => s.Id == id);
    }

    public IEnumerable<Student> GetStudents() {
      return _studentList;
    }

    public Student UpdateStudent(Student student) {

      int index = _studentList.FindIndex(s => s.Id == student.Id);
      if (index > -1) {

        _studentList[index] = student;
      }
      return student;

    }
  }
}
